<!--  -->
<template>
    <page-header-wrapper @back="() => $router.go(-1)">
        <a-card :bordered="false">
            <a-card :bordered="false" title="客户信息">
                <a-button slot="extra" type='primary' v-if="company.origin == 1" @click="showModel()">编辑</a-button>
            <a-descriptions>
                <a-descriptions-item label="客户名称" >{{company.customerName}}</a-descriptions-item>
                <a-descriptions-item label="统一社会信用代码" >{{company.socialCode}}</a-descriptions-item>
                <a-descriptions-item label="创建时间" >{{company.createTime}}</a-descriptions-item>
                <a-descriptions-item label="来源" >{{company.origin == 1 ? '创建' : '平台关联'}}</a-descriptions-item>
                <a-descriptions-item label="手机号" >{{company.contactPhone}}</a-descriptions-item>
                <a-descriptions-item label="姓名" >{{company.contactName}}</a-descriptions-item>
                <a-descriptions-item label="状态" >{{company.state |statusFilter}}</a-descriptions-item>
                <a-descriptions-item label="操作员" >{{company.createdBy}}</a-descriptions-item>
            </a-descriptions>
            </a-card>
            <a-card :bordered="false" title="客户设置">
                <a-button slot="extra" type='primary' @click="update(1)">保存</a-button>
            <a-descriptions>
                <a-descriptions-item label="结算单需要审核" :span="3">
                    <a-radio-group v-model="company.settlementNeedExamine" >
                        <a-radio  v-for="(item,i) in settlementNeedExamineList" :key="i" :value="item.id">{{item.name}}</a-radio>
                    </a-radio-group>
                </a-descriptions-item>
            </a-descriptions>
            </a-card>
        </a-card>

        <a-modal 
            v-model="visible" 
            title="编辑客户"
            @ok="update(0)" 
            @cancel="visible = false">
            <div>
                <a-form-model :model="info" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="客户名称" >
                        <a-input v-model="info.customerName" :disabled="true" allow-clear placeholder="请输入客户名称" />
                    </a-form-model-item>
                    <a-form-model-item label="统一社会信用代码">
                        <a-input v-model="info.socialCode" :disabled="true" allow-clear placeholder="请输入统一社会信用代码" />
                    </a-form-model-item>
                    <a-form-model-item label="姓名" prop="contactName">
                        <a-input v-model="info.contactName" allow-clear placeholder="请输入姓名" />
                    </a-form-model-item>
                    <a-form-model-item label="手机号" prop="contactPhone">
                        <a-input v-model="info.contactPhone" allow-clear placeholder="请输入手机号" />
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>
    </page-header-wrapper> 
</template>

<script>
import { getCustomerDetail , updateCustomerInfo } from '@/api/services/customer';
export default {
    data () {
        return {
            companyId: '',
            company: {
                state: 1
            },
            settlementNeedExamineList: [
                { id: 1, name: '需要'},
                { id: 0, name: '不需要'},
            ],
            info: {

            },
            visible: false,
            rules: {
                contactName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                contactPhone: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'}
                ],
            },
        };
    },
    created(){
        this.companyId = (this.$route.query.companyId)
        this.getDetail();
    },
    methods: {
        // 获取客户详情
        getDetail(){
            let _this = this
            getCustomerDetail(_this.companyId)
            .then(res=>{
                if(res.success){
                    _this.company = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        showModel(){
            this.info = JSON.parse(JSON.stringify(this.company))
            this.visible = true;
        },
        // 保存信息
        update(type){
            let _this = this
            var params = JSON.parse(JSON.stringify(_this.company))
            if(type == 1){
                params.settlementNeedExamine = _this.company.settlementNeedExamine
            }else{
                params = _this.info
            }
            updateCustomerInfo(params)
            .then(res=>{
                if(res.success){
                    _this.getDetail();
                    this.visible = false;
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '修改成功',
                        duration: 8
                    })
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>